/*
 * Created on 2005-9-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.jedi;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class BoardMgr extends BaseObj {
	public BoardMgr() {}
	public Board get(String id) {
		clearErr();
		Board board = new Board(id);
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select * from t_board_info where list_id = ?";
			conn.prepare(sql);
			conn.setString(1,id);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			board.setMsgInfo(get(rs,0,"msg_info"));   
			board.setAuthorId(get(rs,0,"author_id"));
			board.setAuthorName(get(rs,0,"author_name"));
			board.setIssueTime(get(rs,0,"issue_time"));
			return board;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from t_board_info where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			if (getTopNum() > 0)
				sql += " limit " + getTopNum();
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Board board = new Board(get(rs,i,"list_id"));
				board.setMsgInfo(get(rs,i,"msg_info"));   
				board.setAuthorId(get(rs,i,"author_id"));
				board.setAuthorName(get(rs,i,"author_name"));
				board.setIssueTime(get(rs,i,"issue_time"));
				r.add(board);
			}
			return r;
		}
		return null;
	}
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		try {
			conn = new DbConn();
			String sqlCount = "select count(*) as total from t_board_info where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select * from t_board_info where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			Board board = new Board(get(rs,i,"list_id"));
			board.setMsgInfo(get(rs,i,"msg_info"));   
			board.setAuthorId(get(rs,i,"author_id"));
			board.setAuthorName(get(rs,i,"author_name"));
			board.setIssueTime(get(rs,i,"issue_time"));
			r.add(board);
		}
		return r;
	}
}
